package com.busionline.wotu.baselibrary.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.busionline.wotu.baselibrary.R;

/**
 * 支持上拉加载的Recyc1eView
 *
 * @author woochen123
 * @time 2018/5/16 17:34
 * @desc
 */

public class LoadMoreRecycleView extends WrapRecyclerView {
    private static final String TAG = "LoadMoreRecycleView";
    private final Context mContext;
    private boolean isCanLoadMore;//是否可以加载更多
    private OnFooterAutoLoadMoreListener listener;//加载更多事件监听
    private boolean isReClickLoadMore;//是否可以重新加载更多
    private View loadMoreView;

    public void setListener(OnFooterAutoLoadMoreListener listener) {
        this.listener = listener;
    }

    public void setCanLoadMore(boolean canLoadMore) {
        isCanLoadMore = canLoadMore;
    }

    public LoadMoreRecycleView(Context context) {
        this(context, null);
    }

    public LoadMoreRecycleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadMoreRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);
        //添加监听
        addOnScrollListener(mOnScrollListener);
    }

    /**
     * 底部是否可以重新加载更多
     *
     * @param isReClickLoadMore
     */
    public void setIsReClickLoadMore(boolean isReClickLoadMore) {
        this.isReClickLoadMore = isReClickLoadMore;
    }

    //显示底部加载更多
    public void showLoadMore() {
        addLoadMoreView(R.layout.item_footer_loading_more);
        setIsReClickLoadMore(false);
    }

    //加载更多,没有更多的数据了
    public void showNoMoreData() {
        addLoadMoreView(R.layout.item_footer_nomore);
        setIsReClickLoadMore(false);
    }

    //加载更多失败
    public void showLoadMoreError() {
        addLoadMoreView(R.layout.item_footer_load_error);
        setIsReClickLoadMore(true);
    }

    /**
     * 添加上拉加载更多
     *
     * @param loadMoreResId
     */
    private void addLoadMoreView(int loadMoreResId) {
        loadMoreView = LayoutInflater.from(mContext).inflate(loadMoreResId, null);
        addFooterView(loadMoreView);
    }

    /**
     * 移除上拉加载更多
     */
    public void removeLoadMoreView() {
        Log.e(TAG, "removeLoadMoreView: 移除上拉加载视图");
        if (loadMoreView != null) {
            removeFooterView(loadMoreView);
        }
    }


    private OnScrollListener mOnScrollListener = new OnScrollListener() {
        /**
         * 滑动监听
         * @param recyclerView
         * @param dx
         * @param dy
         */
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager instanceof LinearLayoutManager) {
                LinearLayoutManager linearLayout = (LinearLayoutManager) layoutManager;
                int mLastChildPosition = linearLayout.findLastVisibleItemPosition();//最后一个可见条目的索引
                int itemTotalCount = linearLayout.getItemCount();//条目总数
                View lastChildView = linearLayout.getChildAt(linearLayout.getChildCount() - 1);//最后一个条目
                int lastChildBottom = lastChildView.getBottom();//最后一个条目底部距离父容器顶部的距离
                int recyclerBottom = getBottom();//recycleView底部距离父容器顶部的距离
                if (isCanLoadMore && mLastChildPosition == itemTotalCount - 2 && lastChildBottom == recyclerBottom) {
                    //最后一个条目,并且拉到了最底部
                    if ( listener != null) {
                        //业务代码
                        listener.loadMore();
                        Log.e(TAG, "onScrolled: 加载更多" );
                    }
                }
            }
        }
    };

    public interface OnFooterAutoLoadMoreListener {
        //自动加载更多
        void loadMore();

        //加载出错,点击重新加载
        void reLoadMore();
    }

}
